package uuu

import (
	"crypto/md5"
	"encoding/hex"
	"math/rand"
	"os"
	"time"
)

const (
	NUmStr  = "0123456789"
	CharStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
	SpecStr = "+=-@#~,.[]()!%^*$"
)

// password format username**password(sha256)
type Userdata struct {
	Name     string `json:"name"`
	PassWord string `json:"pass_word"`
}

func CreatePassword() Userdata {

	return cpsd()

}

func checkFileIsExist(filename string) bool {
	var exist = true
	if _, err := os.Stat(filename); os.IsNotExist(err) {
		exist = false
	}
	return exist
}

func cpsd() Userdata {

	if checkFileIsExist("userdata") {
		os.Remove("userdata")
	}

	rand.Seed(time.Now().UnixNano())
	u := Userdata{}
	u.Name = "user0226"
	u.PassWord = string(rand.Intn(8))
	h := md5.New()
	h.Write([]byte(u.PassWord)) // 需要加密的字符串为 123456
	cipherStr := h.Sum(nil)
	ud := u.Name + "**" + hex.EncodeToString(cipherStr)
	os.WriteFile("userdata", []byte(ud), 0666)
	return u
}
